﻿using B.S.BasicData.Api.Read.Application.Command.Account;
using B.S.BasicData.Api.Read.DTOs;
using B.S.BasicData.Api.Read.Extensions;
using B.S.BasicData.Api.Read.Filters;
using B.S.BasicData.ErrorCode;
using MD5Hash;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BasicData.Api.Read.Controllers
{
    /// <summary>
    /// 账号控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private readonly IMediator mediator;

        public AccountController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]       
        public async Task<ApiResult<AccountDTO>> Login(LoginAccountCommand request) { 
            return await mediator.Send(request);
        }


        /// <summary>
        /// 决定本周末干啥的接口
        /// </summary>
        /// <returns>学习还是运动</returns>
        //[Authorize]//不好使
        [AccessTokenFilter]
        [HttpGet]
        public string GetWeekDayWrok() {
            return (new Random().Next()) % 2 == 1 ? "学习" : "运动";
        }

        /// <summary>
        /// 获取一个盐值
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetSalt() {

            return this.CreateSalt();
        }

        /// <summary>
        /// 刷新token 接口
        /// </summary>
        /// <param name="request">刷新tokne和指纹</param>
        /// <returns>loginDTO 包装</returns>
        [HttpGet]
        public  Task<ApiResult<AccountDTO>> RefreshToken([FromQuery]RefreshTokenCommand request) {
            return mediator.Send(request);
        }

        /// <summary>
        /// 分页查询账号列表
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <returns></returns>
        [HttpGet]
        //[AccessTokenFilter]
        public   Task<ApiPaging<AccountListDTO>> GetAccountPage([FromQuery]GetAccountPageCommand request) {
            return mediator.Send(request);
        }


    }
}
